//
//  Possession.m
//  RandomPossessions
//
//  Created by bhardy on 7/29/09.
//  Copyright 2009 Big Nerd Ranch. All rights reserved.
//

#import "Possession.h"


@implementation Possession
 
@synthesize possessionName, serialNumber, valueInDollars, dateCreated,imageKey;

-(void) dealloc
{
    [thumbnail release];
    [thumbnailData release];
    [possessionName release];
    [serialNumber release];
    [dateCreated release];
    [imageKey release];
    [super dealloc];
}

+ (id)randomPossession 
{
	NSArray *randomAdjectiveList = [NSArray arrayWithObjects:@"Fluffy",
														  @"Rusty",
														  @"Shiny", nil];
	NSArray *randomNounList = [NSArray arrayWithObjects:@"Bear",
														@"Spork",
														@"Mac", nil];
	
	int adjectiveIndex = random() % [randomAdjectiveList count];
	int nounIndex = random() % [randomNounList count];
	NSString *randomName = [NSString stringWithFormat:@"%@ %@",
								[randomAdjectiveList objectAtIndex:adjectiveIndex],
								[randomNounList objectAtIndex:nounIndex]];
	int randomValue = random() % 100;
	NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c", 
													'0' + random() % 10,
													'A' + random() % 26,
													'0' + random() % 10,
													'A' + random() % 26,
													'0' + random() % 10];
	Possession *newPossession = 
		[[self alloc] initWithPossessionName:randomName
							  valueInDollars:randomValue
								serialNumber:randomSerialNumber];
	return newPossession;
}

- (id)initWithPossessionName:(NSString *)name 
							valueInDollars:(int)value
								serialNumber:(NSString *)sNumber
{
	// Call the superclass's designated initializer 
	self = [super init];
	
	// Did the superclass's initialization fail? 
	if (!self)
		return nil;
	
	// Give the instance variables initial values 
	[self setPossessionName:name]; 
	[self setSerialNumber:sNumber]; 
	[self setValueInDollars:value];
	dateCreated = [[NSDate alloc] init];
	
	// Return the address of the newly initialized object
	return self;
}

- (id)initWithPossessionName:(NSString *)name {
	return [self initWithPossessionName:name 
						 valueInDollars:0
						   serialNumber:@""];
}
- (id)init
{
    return [self initWithPossessionName:@"Possession"
                         valueInDollars:0
                           serialNumber:@""];
}
- (NSString *)description {
	NSString *descriptionString = [[NSString alloc] initWithFormat:@"%@ (%@): Worth $%d, Recorded on %@",
																 possessionName, serialNumber, valueInDollars, dateCreated];
	return descriptionString;
}

-(void) encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:possessionName forKey:@"possessionName"];
    [aCoder encodeObject:serialNumber forKey:@"serialNumber"];
    [aCoder encodeObject:dateCreated forKey:@"dateCreated"];
    [aCoder encodeObject:imageKey forKey:@"imageKey"];
    [aCoder encodeInt:valueInDollars forKey:@"valueInDollars"];
    [aCoder encodeObject:thumbnailData forKey:@"thumbnailData"];
}

-(id) initWithCoder:(NSCoder *)aDecoder
{
    self=[super init];
    if(self)
    {
        [self setPossessionName:[aDecoder decodeObjectForKey:@"possessionName"]];
        [self setSerialNumber:[aDecoder decodeObjectForKey:@"serialNumber"]];
        [self setImageKey:[aDecoder decodeObjectForKey:@"imageKey"]];
        [self setValueInDollars:[aDecoder decodeIntForKey:@"valueInDollars"]];
        dateCreated=[[aDecoder decodeObjectForKey:@"dateCreated"]retain];
        thumbnailData=[[aDecoder decodeObjectForKey:@"thumbnailData"]retain];
    }
    return self;
}

-(UIImage *)thumbnail
{
    if(!thumbnailData)
    {
        return nil;
    }
    if(!thumbnail)
    {
        thumbnail=[[UIImage imageWithData:thumbnailData]retain];
    }
    return thumbnail;
}

+(CGSize) thumbnailSize
{
    return CGSizeMake(40,40);
}

-(void) setThumbnail:(UIImage *)image
{
    [image retain];
    [thumbnail release];
    thumbnail = image;
}

-(void) setThumbnailData:(NSData *)d
{
    [d retain];
    [thumbnailData release];
    thumbnailData = d;
}

-(void) setThumbnailDataFromImage:(UIImage *)image
{
    CGSize origImageSize=[image size];
    
    CGRect newRect;
    newRect.origin=CGPointZero;
    newRect.size=[[self class] thumbnailSize];
    
    float ratio=MAX(newRect.size.width/origImageSize.width, newRect.size.height/origImageSize.height);
    
    UIGraphicsBeginImageContext(newRect.size);
    
    UIBezierPath *path=[UIBezierPath bezierPathWithRoundedRect:newRect cornerRadius:5.0];
    
    [path addClip];
    
    CGRect projectRect;
    projectRect.size.width=ratio*origImageSize.width;
    projectRect.size.height=ratio*origImageSize.height;
    projectRect.origin.x=(newRect.size.width-projectRect.size.width)/2.0;
    projectRect.origin.y=(newRect.size.height-projectRect.size.height)/2.0;
    
    [image drawInRect:projectRect];
    
    UIImage *small=UIGraphicsGetImageFromCurrentImageContext();
    [self setThumbnail:small];
    
    NSData *data=UIImagePNGRepresentation(small);
    [self setThumbnailData:data];
    
    UIGraphicsEndImageContext();
    
}



@end
